package com.nexus.cloud.nacos;

import com.nexus.core.utils.PropertiesUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;

import java.util.HashMap;
import java.util.Properties;

/**
 * 加载nacos配置
 *
 * @Date 2025/9/18 02:03
 * @Author luzhengning
 **/
public class NacosEnvironmentPostProcessor implements EnvironmentPostProcessor {

    /**
     * nacos配置文件名称
     *
     * @Date 2025/9/22 00:56
     * @Author luzhengning
     **/
    private final static String NacosProperties = "nacos.properties";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        //读取当前环境下的nacos配置
        Properties properties = PropertiesUtils.convertReourceOnEnv(NacosProperties);
        //删除当前模块重复的Nacos配置项
        PropertiesUtils.removeRepeatConfig(NacosProperties, properties);
        environment.getPropertySources().addLast(
                new MapPropertySource("NacosPropertiesConfig", new HashMap(properties))
        );
        System.out.println("Nacos本地配置文件加载完成");
    }


}